/**
 * alert.js
 * @fileOverview Contains the javascript code that controls the behavior of the elements in alert.html.
 * @author Matthew George
 * @version 1.3.6
 * 
 * @license Copyright 2009-2010 Matthew George
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** @description Reference to the extension's background page.
 *  @type {*}
 * 	@private
 */
var bkpage;

/** @description The id of the parent tab.
 *	@type {string}
 *	@private
 */
var tabid;

/** @description The url of the parent tab.
 *	@type {string}
 *	@private
 */
var taburl;

/** @description The error description string.
 *	@type {string}
 *	@private
 */
var errorInfo;

/** @description The archive keywords.
 *	@type {Array.<string>}
 *	@private
 */
var archiveKeys;

/** @description The archive names.
 *	@type {Array.<string>}
 *	@private
 */
var archiveNames;

/** @description Load archive info for buttons.
 *	@param {string} errorCode The http status/error code triggering the alert.
 *	@private
 */
function initializeBtns(errorCode) {
	var archives = bkpage.getAlertArchives(errorCode);
	
	archiveKeys = archives[0];
	archiveNames = archives[1];
}

/** @description Return error information.
 *	@return {string} The error information.
 */
function getErrorInfo() {
	return errorInfo;
}

/** @description Return an archive name.
 *	@param {number} index The archive index number.
 *	@return {string} The archive name.
 */
function getArchiveNames(index) {
	return archiveNames[index];
}

/** @description Obtain the variables embedded in the alert url string.
 *	@return {Array.<string>} An array of the variables.
 *	@private
 */
function getUrlVariables() {
	var variableString;
	var splitStrArray;
	var variableArray = new Array();
	
	//get variable string from document url
	variableString = document.URL.substring(document.URL.indexOf('?')+1, document.URL.length);
	
	//split the variable string
	splitStrArray = variableString.split('&');
	
	//seperate keys from values and store in variableArray
	for(var i in splitStrArray) {
		var tempArray = splitStrArray[i].split('=');
		variableArray[tempArray[0]] = tempArray[1];
	}
	
	//return variableArray
	//valid keys: tabid, taburl, errorcode
	return variableArray;
}

/** @description The page initilization function.
 */
function init() {
	//get variables from url
	var urlVars = getUrlVariables();
	
	//get reference to background page
	bkpage = chrome.extension.getBackgroundPage();
	
	//set tab id
	tabid = urlVars['tabid'];
	
	//set tab url
	//if trigger is the options page, set to 'null'
	if(urlVars['taburl'].substr(0,7) == 'http://') {
		taburl = urlVars['taburl'].substr(7);
	}
	else {
		taburl = null;
	}
	
	//get and set error info
	errorInfo = urlVars['errorcode'] + ' ERROR: ' + bkpage.getErrorDesc(urlVars['errorcode']);
	
	//initialize search buttons
	initializeBtns(urlVars['errorcode']);
}

/** @description Change the tab focus to the alert's parent tab.
 */
function goToTab() {
	bkpage.changeSelectedTab(tabid);
}

/** @description Search an archive.
 */
function searchArchive(archive) {
	bkpage.searchArchive(taburl, archiveKeys[archive]);
}